package spring.cloud.monitor.server.controller;

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class HelloController {
    @Value("${server.port}")
    private int port;

    @Value("${spring.application.name}")
    private String name;

    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @RequestMapping(value = "/headers", method = {RequestMethod.GET})
    public String getHttpHeaders() {
        String url = "http://localhost:" + port + "/health/";
        System.out.println(port);
        return restTemplate.getForEntity(url, String.class).getBody();
    }

    @ResponseBody
    @RequestMapping(value = "/hello", method = {RequestMethod.GET})
    public String hello(@RequestParam(value = "name", defaultValue = "Monitor") final String name) {
        return "Hello " + name + "!";
    }
}